package com.example.demo.service.impl;

import com.example.demo.entity.News;
import com.example.demo.entity.Question;
import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.INewsMapper;
import com.example.demo.mapper.IQuestionMapper;
import com.example.demo.param.vo.NewsArrVO;
import com.example.demo.param.vo.NewsVO;
import com.example.demo.param.vo.QuestionVO;
import com.example.demo.service.INewsService;
import com.example.demo.service.ITokenService;
import com.example.demo.util.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired private INewsMapper newsMapper;
    @Autowired private IQuestionMapper questionMapper;

    @Override
    public Boolean insert(String id, String userId, String newsContent) throws ServiceException {
        if (Judge.isNull(id) || Judge.isNull(newsContent)){
            throw new ServiceException("请务必完善必填项！");
        }
        QuestionVO questionVO = this.questionMapper.getById(id);
        if(questionVO == null){
            throw new ServiceException("未找到这个问题");
        }
        Question question = questionVO.getQuestion();
        if(question == null){
            throw new ServiceException("未找到这个问题");
        }
        Integer affect = this.newsMapper.insert(
                Judge.getUUID(),userId,question.getUid(),0,question.getTitle(),newsContent,0,0,new Date());
        if (affect != 1){
            throw new ServiceException("新增失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean insertNotice(String title, String userId, String content) throws ServiceException {
        if(Judge.isNull(title) || Judge.isNull(content)){
            throw new ServiceException("请务必完善必填项");
        }
        Integer affect = this.newsMapper.insert(
                Judge.getUUID(),userId,null,1,title,content,0,0,new Date());
        if (affect != 1){
            throw new ServiceException("新增失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(String id) throws ServiceException {
        if(Judge.isNull(id)){
            throw new ServiceException("请提供有效id");
        }
        NewsVO newsVO = this.newsMapper.getById(id);
        if (newsVO == null){
            throw new ServiceException("未找到指定内容");
        }
        News news = newsVO.getNews();
        if (news == null || news.getNewsType() == 1){
            throw new ServiceException("未找到指定内容");
        }
        news.setIsDelete(1);
        Integer affect = this.newsMapper.update(news.getId(),news.getNewsContent(),
                                                news.getIsWatch(),news.getNewsTile());
        if (affect != 1){
            throw new ServiceException("删除失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteByNotice(String id) throws ServiceException {
        if(Judge.isNull(id)){
            throw new ServiceException("请提供有效id");
        }
        NewsVO newsVO = this.newsMapper.getById(id);
        if (newsVO == null){
            throw new ServiceException("未找到指定内容");
        }
        News news = newsVO.getNews();
        if (news == null || news.getNewsType() == 0){
            throw new ServiceException("未找到指定内容");
        }
        news.setIsDelete(1);
        Integer affect = this.newsMapper.update(news.getId(),news.getNewsContent(),
                news.getIsWatch(),news.getNewsTile());
        if (affect != 1){
            throw new ServiceException("删除失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean update(String id, String userId, String newsTitle,
                          String newsContent) throws ServiceException {
        if(Judge.isNull(id)){
            throw new ServiceException("请提供有效id");
        }
        NewsVO newsVO = this.newsMapper.getById(id);
        if (newsVO == null){
            throw new ServiceException("未找到指定内容");
        }
        News news = newsVO.getNews();
        if (news == null){
            throw new ServiceException("未找到指定内容");
        }
        if (!news.getUserId().equals(userId)){
            throw new ServiceException("不能修改他人消息");
        }
        if(newsTitle != null){
            news.setNewsTile(newsTitle);
        }
        if(newsContent != null){
            news.setNewsContent(newsContent);
        }
        Integer affect = this.newsMapper.update(
                news.getId(),news.getNewsContent(),news.getIsWatch(),news.getNewsTile());
        if (affect != 1){
            throw new ServiceException("修改消息失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public NewsVO getById(String id) throws ServiceException {
        if (Judge.isNull(id)){
            throw new ServiceException("请提供有效的id");
        }
        NewsVO newsVO = this.newsMapper.getById(id);
        if (newsVO == null){
            return null;
        }
        News news = newsVO.getNews();
        news.setIsWatch(1);
        Integer affect = this.newsMapper.update(
                news.getId(),news.getNewsContent(),news.getIsWatch(),news.getNewsTile());
        if (affect != 1){
            throw new ServiceException("浏览失败");
        }
        return newsVO;
    }

    @Override
    public NewsArrVO getQueryPage(String userId, String newsTitle,
                                  String newsContent, Integer page, Integer pageSize) throws ServiceException{
        if (page == null || page < 1 || pageSize == null || pageSize < 1){
            throw new ServiceException("请正确填写分页");
        }
        NewsArrVO newsArrVO = this.newsMapper.getQueryPage(userId,newsTitle,newsContent,page,pageSize);
        newsArrVO.setPage(page);
        newsArrVO.setPageSize(pageSize);
        return newsArrVO;
    }
}
